﻿using Tesla.Scene;
using Tesla.Scene.Shape;
using Tesla.Graphics;
using Tesla.Math;
using Tesla.Core;
using Tesla.Input;
using Tesla.Util;
using Tesla.Scene.Extension;
using Tesla.Content;
using System;
using System.IO;
using Tesla.Content.Loaders;

namespace TeslaSamples.Effects {
    [AppDescription("Normal Mapping", PlatformCapabilities.Direct3D10 | PlatformCapabilities.XNA4, "NormalMapImage", "NormalMapDesc", "NormalMapSource")]
    public class NormalMapSample : BasicApp {
        protected override void LoadContent() {
            ClearColor = Color.Black;
            MemoryStream ms = new MemoryStream();
            BinaryExporter exporter = new BinaryExporter();
            BinaryLoader loader = new BinaryLoader();


            Window.Title = "Normal Mapping Sample";
            ModelLoaderParameters mlp = new ModelLoaderParameters();
            mlp.SwapWindingOrder = true;
            mlp.PreferLitMaterials = false;
            mlp.GenerateTangentBasis = true;
            mlp.NormalGeneration = NormalGeneration.Crease;
           

            Spatial model = ContentManager.Load<Spatial>("Models//starship.dxs", mlp);

            model.SetScale(.15f);
            //model.Material = ContentManager.Load<Material>("NormalMap.tem");
           // model.Material.SetParameter("DiffuseMap", ContentManager.Load<Texture2D>("Textures//rock_diff.dds"));
          //  model.Material.SetParameter("NormalMap", ContentManager.Load<Texture2D>("Textures//rock_norm.dds"));
           // model.Material.SetParameter("MatSpecular", Color.Gray.ToVector3());
         //   model.AddController(new RotateController(new Vector3(.5f, .5f, 0), 25f));
            //model.ComputeTangentBasis();
           // RootNode.AddChild(model);
            RootNode.SetModelBound(new Tesla.Bounding.BoundingBox());
            
            model = ContentManager.Load<Spatial>("Models//statue.tebo");
            Material mat = ContentManager.Load<Material>("NormalMap.tem").Clone();
            mat.SetParameter("DiffuseMap", ContentManager.Load<Texture2D>("Textures//statue_diff.dds"));
            mat.SetParameter("NormalMap", ContentManager.Load<Texture2D>("Textures//statue_norm.dds"));
            mat.SetParameter("MatSpecular", new Vector3(.3f, .3f, .3f));
            model.SetMaterial(mat);
            model.Rotation = Quaternion.FromAngleAxis(MathHelper.ToRadians(-90), Vector3.Up);
            model.SetModelBound(new Tesla.Bounding.BoundingBox());
            RootNode.AddChild(model);
            
            /*
            model = ContentManager.Load<Spatial>("Models//statue.dxs", mlp);
            model.Rotation = Quaternion.FromAngleAxis(MathHelper.ToRadians(-90), Vector3.Up);
            FileStream fs = File.OpenWrite("C:\\Users\\Nicholas Woodfield\\Desktop\\D3D10\\statue2.tebo");
            exporter.Save(model, fs);
            fs.Close();*/
        }
    }
}
